//
//  XMUserExistAlterView.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/4/26.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMUserExistAlterView.h"

@interface XMUserExistAlterView (){
    ExistAlterViewResultBlock _resultBlock;
}

//背景view
@property(nonatomic,strong) UIControl *coverControl;
//内容view
@property (nonatomic,strong) UIView * contentView;


//仍然新建
@property (nonatomic,strong) UIButton * sureCreateBtn;
//查看重复用户
@property (nonatomic,strong) UIButton * toSeeExistBtn;
//直接录入
@property (nonatomic,strong) UIButton * enteringBtn;
@end

@implementation XMUserExistAlterView

//选择展示回调
+ (void)showExistAlterViewWithresultBlock:(ExistAlterViewResultBlock)resultBlock{
    
    
    XMUserExistAlterView * alterView = [[XMUserExistAlterView alloc]initResultBlock:resultBlock];

    [alterView show];

}

-(instancetype)initResultBlock:(ExistAlterViewResultBlock)resultBlock{
    
    if (self = [super init]) {
        _resultBlock = resultBlock;
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    self.backgroundColor = kClearColor;
    
    
    //背景control
    [self.coverControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
    //内容
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@298);
        make.height.equalTo(@(315));
    }];
}


#pragma mark 动画

- (void)show{
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(keyWindow);
    }];
    
    self.contentView.alpha = 0;
    self.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        weakSelf.contentView.alpha = 1;
    }];
}

- (void)hide
{
    MJWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        
        weakSelf.contentView.alpha = 0;
        weakSelf.coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
    } completion:^(BOOL finished) {
        [self.coverControl removeAllSubviews];
        [self.coverControl removeFromSuperview];
        [self.contentView removeAllSubviews];
        [self.contentView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

#pragma mark ----action----




-(void)clickActionWithBtn:(UIButton *)button{
    if (_resultBlock) {
        _resultBlock(button.tag);
    }
    [self hide];
    
}

#pragma mark ----getting&setting----

//背景颜色control
-(UIControl *)coverControl{
    if (!_coverControl) {
        _coverControl = [[UIControl alloc]init];
        _coverControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [self addSubview:_coverControl];
        
    }
    return _coverControl;
}

//内容
-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = kWhiteColor;
        _contentView.layer.cornerRadius = 4.0f;
        _contentView.clipsToBounds = YES;
        //图标
        UIImageView * warningImageView  = [[UIImageView alloc]init];
        [warningImageView setImage:kImage(@"errorAlter")];
        [_contentView addSubview:warningImageView];
        
        [warningImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_contentView).offset(24);
            make.centerX.equalTo(_contentView);
            make.width.equalTo(@54);
            make.height.equalTo(@48);

        }];
        
        //标题
        UILabel * titleLabel  = [[UILabel alloc]init];
        titleLabel.textColor = kTextBlackColor;
        titleLabel.font = kFont(16);
        titleLabel.text = @"用户已存在";
        [_contentView addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(warningImageView.mas_bottom).offset(22);
            make.centerX.equalTo(_contentView);
        }];
        
        
        //仍然创建
        [self.sureCreateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(24);
            make.centerX.equalTo(_contentView);
            make.width.equalTo(@200);
            make.height.equalTo(@40);
        }];
        
        //查看重复用户
        [self.toSeeExistBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.sureCreateBtn.mas_bottom).offset(16);
            make.centerX.width.height.equalTo(self.sureCreateBtn);
        }];
        
        //直接录入
        [self.enteringBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.toSeeExistBtn.mas_bottom).offset(16);
            make.centerX.width.height.equalTo(self.sureCreateBtn);
        }];
        
        
        [self addSubview:_contentView];
    }
    return _contentView;
    
}

//仍然新建按钮
-(UIButton *)sureCreateBtn{
    if (!_sureCreateBtn) {
        _sureCreateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureCreateBtn setTitle:@"仍然新建" forState:UIControlStateNormal];
        _sureCreateBtn.titleLabel.font = kFont(14);
        [_sureCreateBtn setTitleColor:kMainBlueColor forState:UIControlStateNormal];
        _sureCreateBtn.layer.borderColor = kMainBlueColor.CGColor;
        _sureCreateBtn.layer.borderWidth = 1;
        _sureCreateBtn.layer.cornerRadius = 20;
        _sureCreateBtn.clipsToBounds = YES;
        _sureCreateBtn.tag = 1;
        [_sureCreateBtn addTarget:self action:@selector(clickActionWithBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_sureCreateBtn];
    }
    return _sureCreateBtn;
}

//查看重复用户按钮
-(UIButton *)toSeeExistBtn{
    if (!_toSeeExistBtn) {
        _toSeeExistBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_toSeeExistBtn setTitle:@"查看重复用户" forState:UIControlStateNormal];
        [_toSeeExistBtn setTitleColor:kTextGrayColor forState:UIControlStateNormal];
        _toSeeExistBtn.titleLabel.font = kFont(14);
        [_toSeeExistBtn setTitleColor:kMainBlueColor forState:UIControlStateNormal];
        _toSeeExistBtn.layer.borderColor = kMainBlueColor.CGColor;
        _toSeeExistBtn.layer.borderWidth = 1;
        _toSeeExistBtn.layer.cornerRadius = 20;
        _toSeeExistBtn.clipsToBounds = YES;
        _toSeeExistBtn.tag = 2;
        [_toSeeExistBtn addTarget:self action:@selector(clickActionWithBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_toSeeExistBtn];
    }
    return _toSeeExistBtn;
}

//直接录入按钮
-(UIButton *)enteringBtn{
    if (!_enteringBtn) {
        _enteringBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_enteringBtn setTitle:@"直接录入" forState:UIControlStateNormal];
        [_enteringBtn setTitleColor:kTextGrayColor forState:UIControlStateNormal];
        _enteringBtn.titleLabel.font = kFont(14);
        [_enteringBtn addTarget:self action:@selector(clickActionWithBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_enteringBtn setTitleColor:kMainBlueColor forState:UIControlStateNormal];
        _enteringBtn.layer.borderColor = kMainBlueColor.CGColor;
        _enteringBtn.layer.borderWidth = 1;
        _enteringBtn.layer.cornerRadius = 20;
        _enteringBtn.clipsToBounds = YES;
        _enteringBtn.tag = 3;
        [self.contentView addSubview:_enteringBtn];
    }
    return _enteringBtn;
}


@end
